package swagger.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import common.util.BeanUtil;

public class ChapterSwaggerModel extends SwaggerModelObject {

	private Integer chapterId;
	
	private Integer courseId;
	
	private String videoId;
	
	private String chapterName;
	
	private Float price;
	
	private String chapterOrder;
	
	private Integer playCount;
	
	private Integer likeCount;
	
	private List<ChapterSwaggerModel> chapterData;
	
	protected Object getId() {
		return chapterId;
	}

	public boolean hasChildren(){
		return null != chapterData && ! chapterData.isEmpty();
	}
	public boolean isRoot(){
		return null == chapterOrder || chapterOrder.length() == 2;
	}
	
	public List<ChapterSwaggerModel> getChildren(List<Map<String, Object>> list){
		List<ChapterSwaggerModel> children = new ArrayList<ChapterSwaggerModel>();
		for (Map<String, Object> item : list){
			ChapterSwaggerModel chapter = BeanUtil.fromMap(item, ChapterSwaggerModel.class);
			if (chapter != null && chapter.getChapterOrder() != null && chapterOrder != null 
					&& chapterOrder.length() < chapter.getChapterOrder().length() 
					&& chapter.getChapterOrder().startsWith(chapterOrder)){
				if(! children.contains(chapter)){
					children.add(chapter);
				}
			}
		}
		return children;
	}
	
	public static List<ChapterSwaggerModel>	fromList(List<Map<String, Object>> list){
		List<ChapterSwaggerModel> chapters = new ArrayList<ChapterSwaggerModel>();
		for (Map<String, Object> item : list){
			ChapterSwaggerModel chapter = BeanUtil.fromMap(item, ChapterSwaggerModel.class);
			if (chapter.isRoot()){
				chapter.setChapterData(chapter.getChildren(list));
				if (! chapters.contains(chapter)){
					chapters.add(chapter);
				}
			}
			
		}
		return chapters;
	}

	public Integer getChapterId() {
		return chapterId;
	}

	public void setChapterId(Integer chapterId) {
		this.chapterId = chapterId;
	}

	public String getChapterName() {
		return chapterName;
	}

	public void setChapterName(String chapterName) {
		this.chapterName = chapterName;
	}

	public Float getPrice() {
		return price;
	}

	public void setPrice(Float price) {
		this.price = price;
	}

	public String getChapterOrder() {
		return chapterOrder;
	}

	public void setChapterOrder(String chapterOrder) {
		this.chapterOrder = chapterOrder;
	}

	public List<ChapterSwaggerModel> getChapterData() {
		return chapterData;
	}

	public void setChapterData(List<ChapterSwaggerModel> chapterData) {
		this.chapterData = chapterData;
	}

	public Integer getCourseId() {
		return courseId;
	}

	public void setCourseId(Integer courseId) {
		this.courseId = courseId;
	}

	public String getVideoId() {
		return videoId;
	}

	public void setVideoId(String videoId) {
		this.videoId = videoId;
	}

	public Integer getPlayCount() {
		return playCount;
	}

	public void setPlayCount(Integer playCount) {
		this.playCount = playCount;
	}

	public Integer getLikeCount() {
		return likeCount;
	}

	public void setLikeCount(Integer likeCount) {
		this.likeCount = likeCount;
	}
}
